layui.define(['jquery', 'kaLog', 'kaInterval'],
    function (exports) {
        var $ = layui.jquery;
        var kaLog = layui.kaLog;
        var kaInterval = layui.kaInterval;
        function getJumpCountdownTime(seconds) {
            let hours = parseInt(seconds / 3600);
            let minutes = parseInt((seconds % 3600) / 60);
            let secondsLeft = seconds % 60;
            hours = parseInt(hours) == 0 ? "" : `<span class="jumpCountdownHtmlRed">` + parseInt(hours) + `</span><span>小时</span>`;
            minutes = parseInt(minutes) == 0 ? "" : parseInt(minutes);
            secondsLeft = parseInt(secondsLeft) == 0 ? "" : `<span class="jumpCountdownHtmlRed">` + parseInt(secondsLeft) + `</span><span>秒</span>`;
            if(minutes != ""){
                if (secondsLeft == "" ) {
                    minutes = `<span class="jumpCountdownHtmlRed">` + minutes + `</span><span>分钟</span>`;
                } else {
                    minutes = `<span class="jumpCountdownHtmlRed">` + minutes + `</span><span>分</span>`;
                }
            }
            return hours + minutes + secondsLeft;
        }
        var kaMsg = new function () {
            this.loading = function () {
                var layerLoadData = {
                    skin: 'ka-loading-layer',
                    icon: 16,
                    shade: 0.01,
                    time: 500000
                };
                var loadIndex = top.layui.layer.msg('数据加载', layerLoadData);
                return loadIndex;
            }
            this.jumpCountdown = function (seconds, content, callBack, timeNumber) { // 加载倒计时
                var jumpCountdownTimeStr = getJumpCountdownTime(seconds);
                // jumpCountdownId
                if (content == undefined || content == "") {
                    content = "正在执行操作，请稍候"
                }
                var timeNumber = parseInt(seconds) * 1000;
                top.layui.layer.msg(`
                    <input type="hidden" id="jumpCountdownTime" name="jumpCountdownTime" value="` + timeNumber + `">
                    <input type="hidden" id="jumpCountdownOldDay" name="jumpCountdownOldDay" value="` + (new Date().getTime()) + `">
                    <div class="jumpCountdown">
                        <span id="jumpCountdownSYHtml">剩余</span>
                        <span id="jumpCountdownHtml">` + jumpCountdownTimeStr + `</span>
                    </div>
                    <div id="jumpCountdownContent">` + content + `</div>`, {
                    skin: 'ka-jumpCountdown-layer', time: timeNumber, shade: [0.1, '#fff'],
                    success: function (layero, index, that) {
                        var res = {};
                        res["intervalName"] = "jumpCountdown"; // 手機號碼
                        kaInterval.init(res.intervalName, res, function (resData) {
                            var jumpCountdownTime = $("#jumpCountdownTime").val();
                            var jumpCountdownOldDay = $("#jumpCountdownOldDay").val();
                            var currTime = new Date().getTime();
                            var syTime = ((parseInt(jumpCountdownOldDay) + parseInt(jumpCountdownTime)) - currTime) / 1000;
                            if (syTime > 0) {
                                $("#jumpCountdownHtml").html(getJumpCountdownTime(syTime));
                                if(seconds > 20){
                                    if(syTime < 20){
                                        $("#jumpCountdownContent").html(`已经20秒以内了`);
                                    }   
                                }
                                if(seconds > 10){
                                    if(syTime < 10){
                                        $("#jumpCountdownContent").html(`请耐心等待`);
                                    }  
                                }
                                if(seconds > 3){
                                    if(syTime < 3){
                                        $("#jumpCountdownContent").html(`即将为你执行操作`);
                                    } 
                                }
                                if(syTime < 1){
                                    $("#jumpCountdownHtml").html(`<span class="jumpCountdownHtmlGreen">正在执行</span>`);
                                    $("#jumpCountdownSYHtml").html("");
                                }     
                            } else {
                                $("#jumpCountdownHtml").html(`<span class="jumpCountdownHtmlGreen">正在执行</span>`);
                                $("#jumpCountdownSYHtml").html("");
                                kaInterval.clear(resData.intervalName, resData);
                            }
                        });
                    }
                }, function () {
                    typeof callBack == "function" && callBack()
                    kaLog.print("success", [{ "成功信息": content }]);
                });
            }
            this.infor = function () { // 信息 蓝色

            }
            this.success = function (msg, content, callBack) { // 信息 绿色
                top.layui.layer.msg(`<div>` + msg + `</div>`, { icon: 1, skin: 'ka-success-layer', time: 2000, shade: [0.1, '#fff'] }, function () {
                    typeof callBack == "function" && callBack()
                    kaLog.print("success", [{ "成功信息": content }]);
                });
            }
            this.error = function (msg, content, callBack) { // 错误 蓝色
                top.layui.layer.msg(`<div>` + msg + `</div>`, { icon: 2, skin: 'ka-error-layer', time: 2000, shade: [0.1, '#fff'] }, function () {
                    typeof callBack == "function" && callBack()
                    kaLog.print("error", [{ "错误信息": content }]);
                });
            }
            this.errorInfor = function () { // 错误 红色
                layer.msg('第二个回调', {
                    skin: 'layui-layer-win10', // 2.8+
                    btnAsync: enableBtnAsync,
                    time: 20000, // 20s 后自动关闭
                    btn: ['提交给管理员', '关闭'],
                    btn1: function (index, layero, that) {
                        var defer = $.Deferred();
                        that.loading(true);
                        sleep(1000).then(defer.resolve);
                        return defer.promise();
                    }
                });
            }
            this.close = function (index) {
                top.layui.layer.close(index);
            }
        };
        exports('kaMsg', kaMsg);
    }
)